<template>
  <el-container>
    <el-aside width="200px">
      <el-menu>
        <el-menu-item index="1">个人信息</el-menu-item>
        <el-menu-item index="2">宠物社区</el-menu-item>
        <el-menu-item index="3">论坛</el-menu-item>
      </el-menu>
    </el-aside>

    <el-container>
      <el-header>
        <h2>个人信息</h2>
      </el-header>

      <el-main>
        <el-form :model="userInfo" label-width="100px">
          <el-form-item label="头像">
            <el-upload
              class="avatar-uploader"
              action="https://jsonplaceholder.typicode.com/posts/"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="userInfo.avatar" :src="userInfo.avatar" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>

          <el-form-item label="用户名">
            <el-input v-model="userInfo.username"></el-input>
          </el-form-item>

          <el-form-item label="修改密码">
            <el-input type="password" v-model="userInfo.password"></el-input>
          </el-form-item>

          <el-form-item label="个性签名">
            <el-input v-model="userInfo.signature"></el-input>
          </el-form-item>

          <el-form-item label="性别">
            <el-radio-group v-model="userInfo.gender">
              <el-radio label="male">男</el-radio>
              <el-radio label="female">女</el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="onSubmit">保存</el-button>
          </el-form-item>
        </el-form>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
  export default {
    name: "Main",
    data() {
      return {
        userInfo: {
          avatar: '',
          username: '',
          password: '',
          signature: '',
          gender: ''
        }
      };
    },
    methods: {
      handleAvatarSuccess(res, file) {
        this.userInfo.avatar = URL.createObjectURL(file.raw);
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;
        if (!isJPG) {
         this.showError('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
         this.showError('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
      onSubmit() {
        console.log(this.userInfo);

      }
    }
  };
</script>

<style scoped>
  .avatar-uploader {
    display: inline-block;
    width: 100px;
    height: 100px;
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    overflow: hidden;
    position: relative;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    line-height: 100px;
    text-align: center;
  }

  .avatar {
    width: 100%;
    height: 100%;
    display: block;
  }
</style>
